<?php
namespace app\models;

use Yii;
use app\models\Basic;

class DealLog extends Basic
{
    
    public static function tableName()
    {
        return '{{%pj8_deal_log}}';
    }

    public function rules()
    {
        return [
            ['member_id', 'required', 'message' => 'member_id不能为空', 'on' => 'add'],
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'member_id最小存储1'],
            ['order_id', 'integer', 'message' => 'order_id必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'order_id最小存储1'],
            ['orderid', 'required', 'message' => 'orderid不能为空', 'on' => 'add'],
            ['orderid', 'string', 'message' => 'orderid必须是字符串',
                         'max' => 32, 'tooLong' => 'orderid最长32位'],
            ['money', 'required', 'message' => 'money不能为空', 'on' => 'add'],
            ['money', 'integer', 'message' => 'money必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'money最小存储1'],
            ['balance', 'integer', 'message' => 'balance必须为数字，或数字字符串', 
                         'min'=>1, 'tooSmall' => 'balance最小存储1'],
            ['pay_status', 'in', 'range' => ['1', '2'],  'message' => 'pay_status格式不正确'],
            ['flag', 'in', 'range' => ['1', '2', '3', '4'],  'message' => 'flag格式不正确'],
            ['way', 'in', 'range' => ['1', '2'],  'message' => 'way格式不正确'],
            ['remark', 'string', 'message' => 'remark必须是字符串',
                      'max' => 256, 'tooLong' => 'remark最长256位'],
            ['callback_header', 'default', 'value' => '', 'on' => 'add'],
            ['callback_header', 'string', 'message' => 'callback_header必须是字符串'],
            ['callback_encrypt_data', 'default', 'value' => '', 'on' => 'add'],
            ['callback_encrypt_data', 'string', 'message' => 'callback_encrypt_data必须是字符串'],
            ['callback_decrypt_data', 'default', 'value' => '', 'on' => 'add'],
            ['callback_decrypt_data', 'string', 'message' => 'callback_decrypt_data必须是字符串'],
            ['callback_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['callback_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'callback_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }

    /*添加*/
    public function addLog($data)
    {
        $this->scenario = 'add';
        $data['DealLog']['add_time'] = date('Y-m-d H:i:s', time());

        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                return $id;
            }
        }
        return false;
    }

    /*
    修改状态
    */
    public function modifyStatus($data, $id)
    {
        // P($id);
        $dealLog = DealLog::find()->select('balance')->where('orderid = :id', [':id' => $id])->one();
        // P($dealLog);
        if(!is_null($dealLog)){
            $data['DealLog']['callback_time'] = date('Y-m-d H:i:s', time());
            $data['DealLog']['balance'] = $dealLog->balance + $data['DealLog']['account'];

            if($this->load($data) and $this->validate()){
                $dealLog->pay_status = 2;
                if(isset($this->callback_header)){
                    $dealLog->callback_header = $this->callback_header;
                }
                if(isset($this->callback_encrypt_data)){
                    $dealLog->callback_encrypt_data = $this->callback_encrypt_data;
                }
                if(isset($this->callback_decrypt_data)){
                    $dealLog->callback_decrypt_data = $this->callback_decrypt_data;
                }
                if(isset($this->balance)){
                    $dealLog->balance = $this->balance;
                }
                if(isset($this->callback_time)){
                    $dealLog->callback_time = $this->callback_time;
                }
                if($dealLog->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    /*获取内容的会员信息*/
    public function getMember()
    {
        $info = $this->hasOne(Member::className(), ['member_id' => 'member_id'])
                    ->select(['{{%pj8_member}}.member_id', 'nickname']);
        return $info;
    }

    /* 获取商品信息 */
    public function getGoods()
    {
        $info = $this->hasOne(Goods::className(), ['goods_id' => 'goods_id'])
                    ->select(['{{%pj8_goods}}.goods_id', 'name']);
        return $info;
    }
    
}